
from selenium import webdriver

if __name__ == '__main__':
    # 为了防止你不确定自己的 chrom 版本，最好两个混合用
    options = webdriver.ChromeOptions()
    # chrom在79版之前用这个
    # options.add_experimental_option("excludeSwitches", ["enable-automation"])
    # options.add_experimental_option('useAutomationExtension', False)
    # 这个是更改 user-agent 的，可有可无
    # options.add_argument(
    #     "user-agent=Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; AcooBrowser; .NET CLR 1.1.4322; .NET CLR 2.0.50727)")
    driver = webdriver.Chrome(options=options)
    # chrom在79版之后用这个
    driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
        "source": """
        Object.defineProperty(navigator, 'webdriver', {
          get: () => undefined
        })
      """
    })
    # options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")  # 前面设置的端口号
    # browser = webdriver.Chrome(executable_path=r'C:\Program Files\Python38\Scripts\chromedriver.exe',
    #                                 options=options)  # executable执行webdriver驱动的文件

    driver.get('https://www.zhihu.com/search?type=topic&q=' + '陌陌')

    time.sleep(30)
    # 这里最好用代码关闭selenium不然的话你的进程里会残留大量的chrom进程
    driver.close()